import {ElLoading, ElMessage} from "element-plus";
import XEUtils from "xe-utils";

/**
 * 二次封装成功信息
 * @param message   - 要输出的信息
 */
export function useSuccessMessage(message: string) {
    useMessage(message, "success")
}

/**
 * 二次封装错误信息
 * @param message   - 要输出的错误
 */
export function useErrorMessage(message: string) {
    useMessage(message, "error")
}

/**
 * 二次封装信息提示框
 * @param message   - 要输出的信息
 * @param type      - 信息框类型
 * @param settings  - 自定义设置
 */
export function useMessage(message: string, type?: any, settings?: any) {
    ElMessage.closeAll();
    message = XEUtils.isEmpty(message) ? "请求失败！":message
    if (message === null){
        message = "数据处理异常！"
    }
    if (!XEUtils.isPlainObject(settings)) settings = {}
    if (XEUtils.isEmpty(type)) type = "success"

    let duration = 0
    switch (type) {
        case undefined:
        case null:
        case "success":
        case "warning":
            duration = 3000;
            break;
        case "error":
            duration = 0;
            ElMessage.closeAll();
            break;
        default:
            console.error("操作提示：类型错误（未定义）")
            return false;
    }
    let init = {
        showClose: true,
        duration,
        message,
        dangerouslyUseHTMLString: true,
        type,
        customClass: "elMessageDiv"
    }
    XEUtils.merge(init, settings)
    ElMessage(init);
}

let loading: any

/**
 * 打开Loading
 */
export function openLoading(settings?: any) {
    if (!XEUtils.isPlainObject(settings)) settings = {}

    let init = {
        lock: true,
        background: "rgba(153,153,153,0.4)"
    }
    XEUtils.merge(init, settings)

    return loading = ElLoading.service(init)
}

/**
 * 关闭Loading
 */
export function closeLoading(dom?: any) {
    if (XEUtils.isEmpty(dom)) {
        loading.close()
    } else {
        dom.close()
    }
}
